package com.ssm.helong.shop.controller;

import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.entity.GoodsAttribute;
import com.ssm.helong.shop.service.GoodsAttributeService;
import com.ssm.helong.shop.util.Result;
import com.ssm.helong.shop.util.ResultGenerator;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Controller
@RequestMapping("/GoodsAttribute")
public class GoodsAttributeController {

    @Autowired
    private GoodsAttributeService goodsAttributeService;


    /***
     * 添加商品
     * @param goodsAttribute
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/5/1
     */
    @RequestMapping(value = "/goods/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestBody GoodsAttribute goodsAttribute){
        if (StringUtils.isEmpty(goodsAttribute.getGoodsName())
                || StringUtils.isEmpty(goodsAttribute.getGoodsIntro())
                || Objects.isNull(goodsAttribute.getGoodsCategoryId())
                || Objects.isNull(goodsAttribute.getGoodsPrice())
                || Objects.isNull(goodsAttribute.getStockNum())){
            return ResultGenerator.genFailResult("含有未填信息！");
        }
        String result =goodsAttributeService.addShopsGoods(goodsAttribute);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }
    @RequestMapping(value = "/goods/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody GoodsAttribute goodsAttribute) {
    
        String result = goodsAttributeService.updateShopsGoods(goodsAttribute);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /***
     * 商品信息
     * @param goodsId
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/5/1
     */
    @GetMapping("/goods/goodsInfo")
    @ResponseBody
    public Result goodsInfo( Integer goodsId) {
        GoodsAttribute goods = goodsAttributeService.getShopsGoods(goodsId);
        if (goods == null) {
            return ResultGenerator.genFailResult(ServiceResultEnum.DATA_NOT_EXIST.getResult());
        }
        return ResultGenerator.genSuccessResult(goods);
    }

    /***
     * 批量修改商品上架状态（0-下架 1-上架）
     * @param goodsIds
     * @param goodsSellStatus
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/5/1
     */
    @RequestMapping("/goods/status")
    @ResponseBody
    public Result status(Integer[] goodsIds,Integer goodsSellStatus) {
        if (goodsIds.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (goodsSellStatus !=0 && goodsSellStatus != 1) {
            return ResultGenerator.genFailResult("状态异常！");
        }
        if (goodsAttributeService.batchUpdateSellStatus(goodsIds, goodsSellStatus)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("修改失败");
        }
    }


}
